
package com.shop.cereshop.app.controller.nearby;

import com.shop.cereshop.app.service.nearby.BuyerNearbyImageService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.nearby.BuyerNearbyImage;
import com.shop.cereshop.commons.param.nearby.BuyerNearbyImageBatchSaveParam;
import com.shop.cereshop.commons.param.nearby.BuyerNearbyImageSaveParam;
import com.shop.cereshop.commons.param.nearby.BuyerNearbyImageUpdateParam;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.vo.nearby.BuyerNearbyImageVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 买家附近的人图片库（仅图片，无相册分类）
 * </p>
 *
 * @author wang.yao
 * @date 2025-09-25
 */
@Slf4j
@RestController
@RequestMapping("/buyerNearbyImage")
@Api(value = "BuyerNearbyImage", tags = "买家附近的人图片库（仅图片，无相册分类）")
public class BuyerNearbyImageController {
    @Resource
    private BuyerNearbyImageService buyerNearbyImageService;

    @PostMapping("/batchAdd")
    @ApiOperation(value = "批量新增或则修改图片")
    public Result<Void> batchAdd(@RequestBody @Valid BuyerNearbyImageBatchSaveParam param, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }
        List<BuyerNearbyImage> entities = new ArrayList<>();
        if (param.getImages() != null) {
            for (BuyerNearbyImageSaveParam item : param.getImages()) {
                BuyerNearbyImage e = new BuyerNearbyImage();
                e.setBuyerUserId(user.getBuyerUserId());
                e.setImageUrl(item.getImageUrl());
                e.setDescription(item.getDescription());
                e.setSortOrder(item.getSortOrder());
                e.setStatus(1);
                e.setIsDeleted(false);
                entities.add(e);
            }
        }
        boolean ok = buyerNearbyImageService.saveBatch(user.getBuyerUserId(), entities);
        return ok ? Result.success() : Result.failed(CoReturnFormat.FAIL);
    }
    @GetMapping("/listByUser")
    @ApiOperation(value = "按用户分页查询图片")
    public Result<Page<BuyerNearbyImageVO>> listByUser(@RequestParam(value = "userId", required = false) String userId,
                                                       @NotNull @RequestParam("page") Integer page,
                                                       @NotNull @RequestParam("pageSize") Integer pageSize,
                                                       HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }
        Long targetUserId;
        if (userId == null || userId.trim().isEmpty() || "null".equalsIgnoreCase(userId.trim())) {
            targetUserId = user.getBuyerUserId();
        } else {
            try {
                targetUserId = Long.valueOf(userId.trim());
            } catch (NumberFormatException e) {
                targetUserId = user.getBuyerUserId();
            }
        }
        Page<BuyerNearbyImage> dataPage = buyerNearbyImageService.listByUser(targetUserId, user.getBuyerUserId(), page,
                pageSize);
        List<BuyerNearbyImageVO> voList = new ArrayList<>();
        if (dataPage.getList() != null) {
            for (BuyerNearbyImage data : dataPage.getList()) {
                BuyerNearbyImageVO vo = new BuyerNearbyImageVO();
                vo.setId(data.getId());
                vo.setBuyerUserId(data.getBuyerUserId());
                vo.setImageUrl(data.getImageUrl());
                vo.setDescription(data.getDescription());
                vo.setSortOrder(data.getSortOrder());
                vo.setStatus(data.getStatus());
                vo.setIsDeleted(data.getIsDeleted());
                vo.setCreatedAt(data.getCreatedAt());
                vo.setUpdatedAt(data.getUpdatedAt());
                voList.add(vo);
            }
        }
        return Result.success(new Page<>(voList, dataPage.getTotal()));
    }
}
